package com.mrogrape.notepad.activity.action.item;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;

import com.mrogrape.notepad.data.ApplicationContext;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.resource.Settings;
import com.mrogrape.notepad.ui.editor.TextEditor;
import com.mrogrape.notepad.ui.filechooser.SaveOptionPane;


public class SaveAsMenuItemAction extends AbstractAction{

    /** serialVersionUID */
    private static final long serialVersionUID = -1L;

    public SaveAsMenuItemAction(){

	super.putValue(Action.NAME, "Save As...");
	super.putValue(Action.SHORT_DESCRIPTION, "Save as a new File");
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

	TextEditor editor = ViewContext.getMainFrame().getCurrentTextEditor();
	if(editor == null){return;}

	SaveOptionPane accessory = ViewContext.getSaveOptionPane();
	accessory.setCharset(editor.getCharset());
	accessory.setSelectedLineSeparator(editor.getLineSeparator());

	File file = ViewContext.getFileChooser().showSaveFileDialog(editor, accessory, null, editor.getFile(), true, true, editor.getContentType(), ApplicationContext.getSupportedContentType());

	if(file != null){
	    editor.setFile(file, false);
	    editor.setCharset(accessory.getCharset());
	    editor.setLineSeparaotr(accessory.getSelectedLineSeparator());
	    editor.saveFile();

	    Settings.getSettings().setLastChoicFilePath(file.getAbsolutePath());
	    ViewContext.getRecentFileMenu().loadOrSaveFile(file);
	}
    }
}
